/**
 * 角色组件授权 - 页面
 * @author yuan_rz
 * @date 2019/01/07 15:23
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        element,
        vueData = {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            treeData: []
        },
        cols = [[
              {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'NAME', width: 80, title: '名称', fixed: 'left', sort: true, search: 'default'}
            , {field: 'MODULE_NAME', width: 80, title: '模块名称'}
            , {field: 'CMP_STATUS', width: 80, title: '组件状态',filterDatas: app.datas_('sys_role_cmp_status')}
            , {field: 'CMP_TYPE', width: 80, title: '组件类型',filterDatas: app.datas_('sys_cmp_type')}
        ]],
        roleId,
        cmpDatas = null,
        selectNode
    ;

    var roleCmps = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'role_cmps',
                _searchs: [{
                    id: 'role_cmps', tableId: 'role_cmps_table', datas: cols[0]
                }]
            }, win);
        },
        initTable: function () {
            table.render({
                elem: app.getByid(doc, 'role_cmps_table'),
                id: 'role_cmps_table',
                cols: cols,
                page: {layout: ['prev', 'page', 'next', 'count']}
            });
        },
        tableReload: function () {
            app.ajax({
                xurl: 'sys/role/getRoleCmpsList',
                xdata: {
                    roleId: roleId,
                    moduleId: selectNode.id
                },
                cb: function (result) {
                    if (result.ok) {
                        cmpDatas = result.data.data;
                        table.reload('role_cmps_table', {
                            data: cmpDatas
                        });
                    }
                }
            });
        },
        _call: function (obj) {
            roleId = obj.roleId;
            vueData.treeData = obj.treeData;
            selectNode = obj.treeData[0];
            self.tableReload();
        },
        _ready: function () {
            table = layui.table;
            this._super({
                vue: [{
                    el: '#role_cmps',
                    id: 'page',
                    data: vueData,
                    searchId: 'role_cmps',
                    methods: {
                        handleNodeClick: function (node) {
                            selectNode = node;
                            self.tableReload();
                        },
                        changeStatus: function(cmp_status){
                            var checkStatus = table.checkStatus('role_cmps_table');
                            if (checkStatus.data.length > 0) {
                                var _xdata = [];
                                checkStatus.data.forEach(function (v) {
                                    var _temp = app.fields_('sys_role_cmp');
                                    _temp.ID = v.ID;
                                    _temp.CMP_STATUS = cmp_status;
                                    _xdata.push(_temp);
                                });
                                app.ajax({
                                    xurl: 'sys/role/changeRoleCmpsStatus',
                                    xdata: _xdata,
                                    loading: true,
                                    toJsonStr: true,
                                    okmsg: '修改状态成功',
                                    cb: function (result) {
                                        if (result.ok) {
                                            result.xdata.forEach(function (v) {
                                                cmpDatas.filterKV({ID: v.ID}).addMap({CMP_STATUS: cmp_status}, true);
                                            });
                                            self.tableReload();
                                        }
                                    }
                                });
                            }
                        },
                        init: function(){
                            app.ajax({
                                xurl: 'sys/role/initRoleCmpsStatus',
                                xdata: {
                                    roleId: roleId,
                                    moduleId: selectNode.id
                                },
                                cb: function (result) {
                                    if (result.ok) {
                                        cmpDatas = result.data.data;
                                        table.reload('role_cmps_table', {
                                            data: cmpDatas
                                        });
                                    }
                                }
                            });
                        }
                    },
                    created: function(){
                        self._setEleIsReady('role_cmps');
                    }
                }]
            });

            self.initTable();

        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new roleCmps();

});